package com.alliky.core.base;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alliky.core.BuildConfig;
import com.alliky.core.R;
import com.alliky.core.api.BaseApi;
import com.alliky.core.config.Config;
import com.alliky.core.global.Global;
import com.alliky.core.log.KLog;
import com.alliky.core.net.interceptors.DebugInterceptor;
import com.alliky.core.router.RoutePath;
import com.alliky.core.utils.ExceptionCrashHandler;
import com.alliky.core.utils.Utils;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.tamsiree.rxkit.RxTool;

/**
 * @Description TODO
 * @Author wxianing
 * @Date 2020-11-14
 */
public class BaseApplication extends MultiDexApplication {

    private static Application mInstance;

    @Override
    public void onCreate() {
        super.onCreate();

        setApplication(this);
//        x.Ext.init(this);
        //初始化RxTool
        RxTool.init(this);
        //多模块分解打包
        MultiDex.install(this);

        //初始化常用配置
        Config.init(this)
                .withLoaderDelayed(500)
                .withApiHost(BaseApi.API_HOST)
                .withWeChatAppId(Global.ThirdPlatform.WECHAT_APP_ID)
                .withWeChatAppId(Global.ThirdPlatform.WECHAT_APP_SECRET)
                .withInterceptor(new DebugInterceptor("test", R.raw.test))
                .configure();

        Fragment fragment = (Fragment) ARouter.getInstance().build(RoutePath.Home.HOME_FRAGMENT).navigation();

//        KLog.i("fragment", fragment + "");

        ExceptionCrashHandler.getInstance().init(this);

        //内存泄漏检测
//        if (!LeakCanary.isInAnalyzerProcess(this)) {
//            LeakCanary.install(this);
//        }

        SmartRefreshLayout.setDefaultRefreshInitializer((context, layout) -> {
            layout.setRefreshHeader(new WaterDropHeader(context));
            layout.setRefreshFooter(new ClassicsFooter(context));
        });
    }

    /**
     * 获得当前app运行的Application
     */
    public static Application getInstance() {
        if (mInstance == null) {
            throw new NullPointerException("Please inherit BasicApplication or call setApplication.");
        }
        return mInstance;
    }


    public static synchronized void setApplication(@NonNull Application application) {
        mInstance = application;
        //初始化工具类
        Utils.init(application);
        ARouter.init(application);
        KLog.init(BuildConfig.DEBUG);

        //注册监听每个activity的生命周期,便于堆栈式管理
        application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                KLog.d("activity>>>", activity.getClass().getSimpleName());
                AppManager.getInstance().addActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {
            }

            @Override
            public void onActivityResumed(Activity activity) {
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                AppManager.getInstance().removeActivity(activity);
            }
        });
    }
}
